package com.weai.portal.controller;

import com.weai.portal.common.ApiResponse;
import com.weai.portal.entity.AiPapers;
import com.weai.portal.service.AiPapersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * AI清单控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/aipapers")
@RequiredArgsConstructor
@Tag(name = "AI清单管理", description = "管理AI资源的创建、查询、查看和编辑")
public class AiPapersController {

    private final AiPapersService aiPapersService;

    @GetMapping
    @Operation(summary = "获取AI清单列表", description = "获取所有AI清单或根据标题搜索")
    public ApiResponse<List<AiPapers>> list(@RequestParam(required = false) String title) {
        log.info("开始获取AI清单列表，标题关键词: {}", title);
        try {
            List<AiPapers> aiPapers;
            if (title != null && !title.isEmpty()) {
                log.debug("使用标题关键词搜索AI清单: {}", title);
                aiPapers = aiPapersService.searchByTitle(title);
            } else {
                log.debug("获取所有AI清单");
                aiPapers = aiPapersService.getAllAiPapers();
            }
            log.info("成功获取AI清单列表，数量: {}", aiPapers.size());
            return ApiResponse.success(aiPapers);
        } catch (Exception e) {
            log.error("获取AI清单列表失败", e);
            return ApiResponse.error(500, "获取AI清单列表失败");
        }
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取AI清单详情", description = "根据ID获取AI清单详情")
    public ApiResponse<AiPapers> getById(@PathVariable Long id) {
        log.info("开始获取AI清单详情，ID: {}", id);
        try {
            AiPapers aiPapers = aiPapersService.getAiPapersById(id);
            log.info("成功获取AI清单详情，ID: {}", id);
            return ApiResponse.success(aiPapers);
        } catch (Exception e) {
            log.error("获取AI清单详情失败，ID: {}", id, e);
            return ApiResponse.error(500, "获取AI清单详情失败");
        }
    }

    @PostMapping
    @Operation(summary = "创建AI清单", description = "创建新的AI清单")
    public ApiResponse<AiPapers> create(@RequestBody AiPapers aiPapers) {
        log.info("开始创建AI清单，标题: {}", aiPapers.getTitle());
        try {
            // TODO: 设置创建人信息
            AiPapers createdAiPapers = aiPapersService.createAiPapers(aiPapers);
            log.info("成功创建AI清单，ID: {}", createdAiPapers.getId());
            return ApiResponse.success(createdAiPapers);
        } catch (Exception e) {
            log.error("创建AI清单失败，标题: {}", aiPapers.getTitle(), e);
            return ApiResponse.error(500, "创建AI清单失败");
        }
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新AI清单", description = "更新指定ID的AI清单")
    public ApiResponse<AiPapers> update(@PathVariable Long id, @RequestBody AiPapers aiPapers) {
        log.info("开始更新AI清单，ID: {}", id);
        try {
            // TODO: 设置更新人信息
            AiPapers updatedAiPapers = aiPapersService.updateAiPapers(id, aiPapers);
            log.info("成功更新AI清单，ID: {}", id);
            return ApiResponse.success(updatedAiPapers);
        } catch (Exception e) {
            log.error("更新AI清单失败，ID: {}", id, e);
            return ApiResponse.error(500, "更新AI清单失败");
        }
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除AI清单", description = "删除指定ID的AI清单")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        log.info("开始删除AI清单，ID: {}", id);
        try {
            aiPapersService.deleteAiPapers(id);
            log.info("成功删除AI清单，ID: {}", id);
            return ApiResponse.success();
        } catch (Exception e) {
            log.error("删除AI清单失败，ID: {}", id, e);
            return ApiResponse.error(500, "删除AI清单失败");
        }
    }
} 